import { color } from "echarts/core";

/**
 *@param {Array} attr = [
            { name: "name", type: "string", value: "武汉" },
            { name: "id", type: "number", value: 100 }
 ]
*@param {Array} position = [114.30,30.50]
*@param {Object} service ={docName,layerId}
 */
class Point {
    static add({
        position,
        attr = [],
        service,
        docLayer,
        color
    }) {
        var gpoint = new Zondy.Object.GPoint(position[0], position[1]);//createPoint();
        //设置当前点要素的几何信息
        var fGeom = new Zondy.Object.FeatureGeometry({ PntGeom: [gpoint] });
        /* 1-2、style */
        var pointInfo = new Zondy.Object.CPointInfo({
            Angle: 0,
            Color: color, //子图颜色
            Space: 0,
            SymHeight: 5, //高度
            SymID: 21,//子图号
            SymWidth: 5 //宽度
        });
        //设置当前点要素的图形参数信息
        var webGraphicInfo = new Zondy.Object.WebGraphicsInfo({
            /* 1-Point 2-LineString 3-Polygon */
            InfoType: 1,
            PntInfo: pointInfo
        });
        /* 1-3、属性 */
        var attValue = attr.map(item => item.value);
        /* new ol.Feature */
        const feature = new Zondy.Object.Feature({
            fGeom: fGeom,
            GraphicInfo: webGraphicInfo,
            AttValue: attValue
        })
        feature.setFType(1);
        /* 二、FeatureSet 创建mapgis的要素集 source */
        var featureSet = new Zondy.Object.FeatureSet();
        //设置属性结构--需要和mapgis数据库中图层的字段映射;
        var cAttStruct = new Zondy.Object.CAttStruct({
            FldName: attr.map(item => item.name),
            FldNumber: attr.length,
            FldType: attr.map(item => item.type)
        });
        featureSet.AttStruct = cAttStruct;
        //添加要素到要素数据集
        featureSet.addFeature(feature);
        /* 三、调用服务 将要素集添加xzd-add地图文档的Point图层中 */
        //创建一个编辑服务类
        /*
         第一个参数地图文档的名称
         第二个参数是添加要素集所在的图层
         */
        var editService = new Zondy.Service.EditDocFeature(service.docName, service.layerId, {

        });
        //执行添加点要素功能
        /* http */
        editService.add(featureSet, Point.onSuccess(docLayer));
    }
    /* 
    1、更新位置
    2、更新属性
     */
    static update({
        position = [],
        attr,
        fid,
        service,
        docLayer,
        color,
    }) {
        /* 修改要素属性(必选参数),位置,样式 */
        //创建一个点形状，描述点形状的几何信息
        let fGeom = null;

        if (position.length) {
            var gpoint = new Zondy.Object.GPoint(position[0], position[1]);
            fGeom = new Zondy.Object.FeatureGeometry({
                PntGeom: [gpoint]
            });
        }

        var pointInfo = new Zondy.Object.CPointInfo({
            Angle: 0,
            Color: color, //子图颜色
            Space: 0,
            SymHeight: 5, //高度
            SymID: 21,//子图号
            SymWidth: 5 //宽度
        });
        //设置当前点要素的图形参数信息
        var webGraphicInfo = new Zondy.Object.WebGraphicsInfo({
            InfoType: 1,
            PntInfo: pointInfo
        });
        /* tips:更新的时候必须写属性值 */
        var attValue = attr.map(item => item.value);
        var newFeature = new Zondy.Object.Feature({
            fGeom: fGeom,
            GraphicInfo: webGraphicInfo,
            AttValue: attValue
        });
        //设置要素为点要素
        newFeature.setFType(1);
        newFeature.setFID(fid); //new-code

        //创建一个点要素数据集
        var featureSet = new Zondy.Object.FeatureSet();
        //设置属性结构
        var cAttStruct = new Zondy.Object.CAttStruct({
            FldName: attr.map(item => item.name),
            FldNumber: attr.length,
            FldType: attr.map(item => item.type)
        });
        featureSet.AttStruct = cAttStruct;
        //添加要素到要素数据集
        featureSet.addFeature(newFeature);
        var editService = new Zondy.Service.EditDocFeature(service.docName,
            service.layerId, {

        });
        /* 发送Http */
        editService.update(featureSet, this.onSuccess(docLayer));
    }
    static onSuccess(docLayer) {
        return function (data) {
            if (data.succeed) {
                alert("操作点要素成功！");
                /* 发送http */
                docLayer.refresh();
            } else {
                alert("操作点要素失败！");
            }
        }
    }
}
export { Point }